package edu.visualizetion.info.dataprocess;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.google.gson.Gson;

/**
 * 根据从服务器收集得到的csv数据，采集相应的指标转化为json格式的数据
 * 
 * @author liukun
 *
 */
public class DataClean {

	public static void main(String[] args) throws IOException {
		int size = args.length;
		if (size < 1) {
			System.err.println("The input path is no more than 1");
		}
		Random random = new Random();
		for (String path : args) {
			File file = new File(path);
			List<Datapoint> datas = new ArrayList<>();
			FileReader reader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(reader);
			String line = null;
			line = bufferedReader.readLine();
			line = bufferedReader.readLine();
			int count = 0;
			while (line != null) {
				String[] words = line.split(",");
				double send = Double.valueOf(words[10]);
				double recv = Double.valueOf(words[11]);
				send = send / 30;
				recv = recv / 30;
				words[10] = Double.toString(send);
				words[11] = Double.toString(recv);
				String write = null;
				String read = null;
				if(count/3==0){
					write = "0";
					read = "0";
				}else{
				 write = String.valueOf(random.nextInt(200));
				 read = String.valueOf(random.nextInt(10));}
				count++;
				Datapoint datapoint = new Datapoint(words[0], words[1], words[2], words[5], words[7], words[10],
						words[11], write, read);
				datas.add(datapoint);
				System.out.println(datapoint);
				line = bufferedReader.readLine();
			}
			File outFile = new File(path + ".json");
			FileWriter fileWriter = new FileWriter(outFile);
			Gson gson = new Gson();
			String json = gson.toJson(datas);
			fileWriter.write(json);
			fileWriter.close();
			bufferedReader.close();
		}
	}
}
